package cn.zcf.basic.controller;

import cn.zcf.basic.exception.BusinessException;
import cn.zcf.basic.utils.JsonResult;
import cn.zcf.org.dto.SmsCodeDto;
import cn.zcf.user.service.IVerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyService verifyService;

    @RequestMapping("/image/{key}")
    public JsonResult imageVerifyCode(@PathVariable("key") String key){
        try {
            String base64Str = verifyService.getImgCode(key);
            return JsonResult.me().setResultObj(base64Str);
        } catch (BusinessException e){ //业务异常捕获
            return JsonResult.me().setMsg("获取失败!"+e.getMessage());
        } catch (Exception e) { //500
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }
    @PostMapping("/smsCode")
    public JsonResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyService.sendSmsCode(smsCodeDto);
            return new JsonResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常,请稍后重试!");
        }
    }
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyService.binderSmsCode(map);
            return JsonResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常，稍后重试");
        }
    }
}